<template>
  <div class="app-container">
    <div class="filter-container">
      <AuthButton auth-name="CreateButton">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="setFormData(null,true)">
          创建根目录
        </el-button>
      </AuthButton>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      lazy
      border
      :load="load"
      :tree-props="{children: 'children' , hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="name"
        label="菜单名称"
        width="180"
      />
      <el-table-column
        prop="icon"
        label="菜单图标"
        width="180"
      />
      <el-table-column
        prop="url"
        label="访问地址"
      />
      <el-table-column
        prop="comName"
        label="组件名称"
      />
      <el-table-column
        prop="sort"
        label="排序"
        width="50"
      />
      <el-table-column
        label="显示"
        width="50"
      >
        <template slot-scope="{row}">
          {{ row.hidden | exchangeStatus }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="400" align="center">
        <template slot-scope="{row}">
          <AuthButton key="1" auth-name="CreateButton">
            <el-button ref="CREATE_BUTTON" type="primary" @click="setFormData(row,true)">创建子菜单</el-button>
          </AuthButton>
          <AuthButton key="2" auth-name="EditButton">
            <el-button type="success" @click="setFormData(row)">编辑 </el-button>
          </AuthButton>
          <AuthButton key="3" auth-name="SettingButton">
            <el-button type="info" @click="showButtonList(row)">配置按钮 </el-button>
          </AuthButton>
          <AuthButton key="d" auth-name="DeleteButton">
            <el-button type="danger" @click="deleteMenu(row)">删除 </el-button>
          </AuthButton>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" style="width: 1100px;margin: auto">
      <el-form ref="dataForm" :rules="rules" :model="fromTemp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="fromTemp.name" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="fromTemp.icon" />
        </el-form-item>
        <el-form-item label="访问地址" prop="url">
          <el-input v-model="fromTemp.url" />
        </el-form-item>
        <el-form-item label="组件名称" prop="comName">
          <el-input v-model="fromTemp.comName" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="fromTemp.sort" />
        </el-form-item>
        <el-form-item label="显示" prop="hidden">
          <el-switch
            v-model="fromTemp.hidden"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="N"
            inactive-value="Y"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveMenu">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="菜单按钮列表" :visible.sync="dialogButtonFormVisible" style="margin: auto">
      <MenuButtonList :menuid="menuId" />
    </el-dialog>
  </div>
</template>
<script>
import { DeleteMenu, GetSystemMenuList, SaveSystemMenu } from '@/api/SystemManagerApi'
import MenuButtonList from '@/views/SystemSetting/SystemMenu/components/MenuButtonList'
import AuthButton from '@/views/authButton/Button'
export default {
  components: { AuthButton, MenuButtonList },
  filters: {
    exchangeStatus: function(value) {
      if (value === 'Y') {
        return '否'
      } else {
        return '是'
      }
    }
  },
  data() {
    return {
      rules: {
        name: [{ required: true, message: 'type is required', trigger: 'change' }],
        icon: [{ required: true, message: 'type is required', trigger: 'change' }],
        url: [{ required: true, message: 'type is required', trigger: 'change' }]
      },
      title: '编辑菜单',
      fromTemp: {
        name: '',
        icon: '',
        url: '',
        id: null,
        comName: null,
        hidden: 'N',
        sort: 0
      },
      dialogFormVisible: false,
      dialogButtonFormVisible: false,
      listLoading: false,
      menuId: null,
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetSystemMenuList().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    load(tree, treeNode, resolve) {
      GetSystemMenuList(tree.id).then(response => {
        const data = response.data
        resolve(data)
      })
    },
    setFormData(row, isCreate) {
      if (isCreate) {
        if (row) {
          this.resetFormData(row.id)
        } else {
          this.resetFormData()
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else {
        // eslint-disable-next-line no-undef
        this.fromTemp = Object.assign({}, row)
      }
      this.dialogFormVisible = true
    },
    resetFormData(parentId) {
      this.fromTemp = {
        name: '',
        icon: '',
        url: '',
        id: null,
        parentId: parentId || null,
        comName: null,
        hidden: 'N',
        sort: 0
      }
    },
    saveMenu() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          SaveSystemMenu(this.fromTemp).then((response) => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    showButtonList(row) {
      this.menuId = row.id
      this.dialogButtonFormVisible = true
    },
    deleteMenu(row) {
      this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DeleteMenu({ id: row.id }).then((res) => {
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
